<template>
<swiper indicator-dots 	autoplay 	:interval="2000" :duration="1000" 	circular>
  <swiper-item v-for="(item, index) in goodsInfo.pics" :key="index">
    <image :src="item.pics_big" @click="preview(index)"></image>
  </swiper-item>
</swiper>
<view class="goods-info-box" v-if="goodsInfo.goods_name">
  <text>{{item}}</text>
  <view class="price">￥{{goodsInfo.goods_price}}</view>
  <view class="goods-info-body">
    <view class="goods-name">{{goodsInfo.goods_name}}</view>
    <view class="favi">
      <uni-icons type="star" size="18" color="gray"></uni-icons>
      <text>收藏</text>
    </view>
  </view>
  <view class="yf">快递：免运费</view>
</view>
<view class="goods-info-detail">
  <rich-text :nodes="goodsInfo.goods_introduce"></rich-text>
</view>
<view class="goods_nav">
  <uni-goods-nav :fill="true" :options="options" :button-group="buttonGroup" @click="onClick" @buttonClick="buttonClick" />
</view>
</template>

<script setup>
  import {ref, reactive, onMounted} from "vue"
  import {onLoad} from "@dcloudio/uni-app"
  import {useCartStore} from "@/store/cart.js"
  
  const cartStore = useCartStore()
  let goodsInfo = ref([])
  const options = [{
    icon: "shop",
    text: "店铺"
  },{
    icon: "cart",
    text: "购物车",
    info: 2
  }]
  const buttonGroup = [{
    text: "加入购物车",
    backgroundColor: "#ff0000",
    color: "#fff"
  },{
    text: "立即购买",
    backgroundColor: "#ffa200",
    color: "#fff"
  }]
  
  onLoad((options) => {
    console.log(options)
    const goodsID = options.goods_id
    getGoodsDetail(goodsID)
  })
  onMounted(() => {
    console.log("onMounted")
  })
  
   async function getGoodsDetail(goodsID){
    const {data} = await uni.$http.get("/api/public/v1/goods/detail", {goods_id: goodsID})
    if(data.meta.status !== 200) return uni.$showToast("数据加载失败")
    //解决图片底部的空白及IOS下无法加载webp的问题
    data.message.goods_introduce = data.message.goods_introduce.replace(/<img/g,"<img style='display: block'").replace(/webp/g, "jpg")
    goodsInfo.value = data.message
  }
  
  function preview(i) {
    uni.previewImage({
      current: i,
      urls:goodsInfo.value.pics.map(item => item.pics_big)
    })
  }
  
  function onClick(e) {
    console.log(e)
    if(e.content.icon === "cart"){
      uni.switchTab({
        url:"/pages/cart/cart"
      })
    }
  }
  function buttonClick(e) {
    if(e.content.text === "加入购物车") {
      const goods = {
        goods_id: goodsInfo.value.goods_id,
        goods_name: goodsInfo.value.goods_name,
        goods_price: goodsInfo.value.goods_price,
        goods_count: 1,
        goods_small_logo: goodsInfo.value.goods_small_logo,
        goods_state: true
      }
      //添加商品到购物车
      cartStore.addToCart(goods)
    }
  }
</script>

<style lang="scss">
swiper {
  height: 750rpx;
  image {
    width: 100%;
    height: 100%;
  }
}
// 商品信息区域的样式
.goods-info-box {
  padding: 10px;
  padding-right: 0;

  .price {
    color: #c00000;
    font-size: 18px;
    margin: 10px 0;
  }

  .goods-info-body {
    display: flex;
    justify-content: space-between;

    .goods-name {
      font-size: 13px;
      padding-right: 10px;
    }
    // 收藏区域
    .favi {
      width: 120px;
      font-size: 12px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      border-left: 1px solid #efefef;
      color: gray;
    }
  }

  // 运费
  .yf {
    margin: 10px 0;
    font-size: 12px;
    color: gray;
  }
}
.goods-info-detail {
  padding-bottom: 50px
}
.goods_nav {
  position: sticky;
  bottom: 0;
}
</style>
